package com.xpgk.xpsoft.role;

import com.jfinal.aop.Before;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.common.controller.BaseController;
import com.xpgk.common.kit.NetKit;
import com.xpgk.common.kit.PubKit;
import com.xpgk.model.FrmUser;
import com.xpgk.xpsoft.common.XPSoftLog;
import com.xpgk.xpsoft.constants.XPSoftConstant;
import com.xpgk.xpsoft.user.UserService;

public class RoleController extends BaseController {

	public void index() {
		int CurUserID = getCurUserID();
		FrmUser user = UserService.getUser(CurUserID);
		if ("2".equals(user.getTYPE()))// 操作员不具备用户管理权限
		{
			renderError(404);
			return;
		}
		setAttr("bCanAdd", UserService.hasRight(CurUserID, "/xpRole/add_ui"));
		setAttr("bCanEdit", UserService.hasRight(CurUserID, "/xpRole/edit_ui"));
		render("index.html");
	}

	/**
	 * 分页数据
	 */
	public void pagingData() {
		int CurUserID = getCurUserID();
		FrmUser user = UserService.getUser(CurUserID);
		Kv cond = Kv.by("NAME like ", isParaBlank("name") ? null : getPara("name") + "%");
		String data = paging("xpSoftRole.findList", Kv.by("cond", cond).set("DEPARTMENTID", user.getDEPARTMENTID()));
		renderJson(data);
	}

	// 注：定义函数名时，如"void add()",页面的展示请以_ui结尾，void add_ui() 涉及到权限问题
	// add与add_ui有同样的权限
	public void add_ui() {
		render("add.html");
	}

	// 注册
	@Before(Tx.class)
	public void add() {
		Record role = GetRecord("role");
		role.set("DEPARTMENTID", UserService.getUser(getCurUserID()).getDEPARTMENTID());
		String result = RoleService.saveRole(role);
		if (!result.equals("ok")) {
			renderJSONMsg(result, false);
			return;
		}
		XPSoftLog.write(getCurUserID(), "addRole", "role=" + role.toString(), XPSoftConstant.log_role, 1, "添加角色", NetKit.getClientIP(getRequest()));
		setAttr("ID", role.getInt("id"));
		renderJSONMsg("添加成功.", true);
	}

	// 修改
	public void edit_ui() {
		int id = getParaToInt("ID", 0);
		Record role = Db.findById("FRM_ROLE", id);
		setAttr("role", role);
		setAttr("bEdit", 1);
		render("edit.html");
	}

	// 修改
	@Before(Tx.class)
	public void edit() {
		Record role = GetRecord("role");
		String result = RoleService.updateRole(role);
		if (!result.equals("ok")) {
			renderJSONMsg(result, false);
			return;
		}
		XPSoftLog.write(getCurUserID(), "editRole", "role=" + role.toString(), XPSoftConstant.log_role, 2, "修改角色信息", NetKit.getClientIP(getRequest()));
		renderJSONMsg("修改成功.", true);
	}

	// 修改权限
	public void right_ui() {
		int id = getParaToInt("ID", 0);
		int bAdd = getParaToInt("bAdd", 0);
		setAttr("id", id);
		setAttr("bAdd", bAdd);
		setAttr("zNode", JsonKit.toJson(RoleService.getRightListForTree(id)));
		render("right.html");
	}

	// 修改角色权限
	@Before(Tx.class)
	public void right() {
		int id = getParaToInt("ID", 0);
		String rights = getPara("rights");
		Integer[] result = null;
		if (!PubKit.isBlank(rights)) {
			String[] rights2 = rights.split(",");
			result = new Integer[rights2.length];
			for (int i = 0; i < result.length; i++)
				result[i] = Integer.parseInt(rights2[i]);
		}
		RoleService.updateRoleRights(id, result);
		XPSoftLog.write(getCurUserID(), "editRoleRight", "rights=" + rights, XPSoftConstant.log_role, 4, "修改角色权限", NetKit.getClientIP(getRequest()));
		renderJSONMsg("设置成功.", true);
	}

	@Before(Tx.class)
	public void editState() {
		int id = getParaToInt("ID", 0);
		int state = getParaToInt("state", -1);
		if (state != -1) {
			Record role = Db.findById("FRM_ROLE", id);
			role.set("STATE", state);
			String result = RoleService.updateRole(role);
			if (!result.equals("ok")) {
				renderJSONMsg(result, false);
				return;
			}
			renderJSONMsg("修改成功.", true);
		} else {
			renderJSONMsg("修改失敗", false);
		}
	}
}
